package com.sky.mapper;

import com.sky.entity.AddressBook;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface AddressBookMapper {

    /**
     * 插入地址
     * @param addressBook
     */
    @Insert("INSERT INTO address_book (user_id, consignee, sex, phone, province_code, province_name, city_code, city_name, district_code, district_name, detail, label, is_default) " +
            "VALUES (#{userId}, #{consignee}, #{sex}, #{phone}, #{provinceCode}, #{provinceName}, #{cityCode}, #{cityName}, #{districtCode}, #{districtName}, #{detail}, #{label}, #{isDefault})")
    void addAddressBook(AddressBook addressBook);

    /**
     * 根据用户ID查询所有地址
     * @param userId
     * @return
     */
    @Select("select * from address_book where user_id = #{userId}")
    List<AddressBook> listByUserId(Long userId);

    /**
     * 查询默认地址
     * @param userId
     * @return
     */
    @Select("select * from address_book where user_id = #{userId} and is_default = 1")
    AddressBook getDefaultAddressBook(Long userId);

    /**
     * 修改地址
     * @param addressBook
     */
    void updata(AddressBook addressBook);


    /**
     * 根据ID删除地址
     * @param id
     * @param userId
     */
    @Delete("delete from address_book where user_id = #{userId} and id = #{id}")
    void deleteById(Long id, Long userId);


    /**
     * 根据ID查询地址
     * @param id
     * @param userId
     * @return
     */
    @Select("select * from address_book where user_id = #{userId} and id = #{id}")
    AddressBook getADDressBookById(Long id, Long userId);
}
